﻿var app = null;

$(document).ready(function() {
    var initializeApplication = function() {
        app = new TMMobile();
        app.setCurrentLanguage();
        app.initialize();

        app.TMMService = new TMMService();

        app.TMMService.Redirect.GetMinVersion(app.Phone.getMobilePlatformName(), function (response) {
            if (app.MobileApplicationVersion > 0 && response && response.MinVersion > app.MobileApplicationVersion) {
                app.Phone.updateApplication(response.AppStoreUrl);
                return;
            }

            Loader.setLoadingScreenPercent(65, "Se initializeaza aplicatia...");
            initializeUser();
        });
    };

    var initializeUser = function() {
        var settings = app.getCurrentSettings();
        var auth = null;
        if (settings.AutoLogin)
            auth = app.getAuthenticationData();

        if (!auth) {
            app.Pages.Authentication.instance = new AuthenticationPage(app.Pages.Authentication.container);
            Loader.setLoadingScreenPercent(100);
            app.changePage(app.Pages.Authentication);
        } else {
            app.setUserData(auth);
            app.TMMService = new TMMService(auth.Token, auth.ServerUrl);

            app.TMMService.TMM.GetTMMData(function(response) {
                Loader.setLoadingScreenPercent(80);
                if (response.Exception) {
                    app.setAuthenticationData(null);
                    document.location.reload();
                } else {
                    app.TMMData = response.FinalResult;

                    Loader.setLoadingScreenPercent(100);
                    app.Pages.Dashboard.instance = new Dashboard(app.Pages.Dashboard.container);
                    app.changePage(app.Pages.Dashboard, false, true);
                    app.registerDeviceId();
                }
            });
        }
    };

    initializeApplication();
});

function translate(key) {
    return RS.Global.translate(key);
};

function picDone() {
    var pictureData = Android.getPictureData();
    if (!pictureData)
        return;
    var picture = JSON.parse(pictureData);
    if (app && picture && app.CurrentPage.instance.savePicture)
        app.CurrentPage.instance.savePicture(picture);
};

function showLeftMenu() {
    if (app)
        app.showLeftMenu();
};

function IphonePic(pic, picName) {
    var picture = new Object();
    picture.pic = pic;
    picture.name = picName || "pic.jpg";
    if (app && app.CurrentPage.instance)
        app.CurrentPage.instance.savePicture(picture);
};

function BlackberryPic(pic) {
    if (app && app.Pages.MyProfile.instance)
        app.CurrentPage.instance.savePicture(pic);
};

function WinPhonePic(pic) {
    if (app && app.Pages.MyProfile.instance)
        app.CurrentPage.instance.savePicture(pic);
};

function updatePushNotificationSubscriptionUrl(subscriptionUrl) {
    app.User.DeviceId = subscriptionUrl;
    if (app.User.Username || app.User.PRS_ID) {
        var username = app.User.IsAlsoEmployee ? null : app.User.Username;
        var settings = app.getCurrentSettings();
        if (settings && !settings.AutoNotifications)
            app.TMMService.PushMessaging.UnregisterDevice(app.User.DeviceId, app.Phone.getMobilePlatformIndex(), Constants.ApplicationId);
        else app.TMMService.PushMessaging.RegisterDevice(app.User.DeviceId, app.Phone.getMobilePlatformIndex(), Constants.ApplicationId, app.User.Company, username, app.User.PRS_ID, settings.Language);
    }
};

function beginModuleAction() {
    if (app && app.CurrentPage && app.CurrentPage.instance)
        app.CurrentPage.instance.showActions();
};

function updateUnreadNotifications() {
    app.updateUnreadNotifications();
    if (app.CurrentPage && app.CurrentPage.instance == app.Pages.Dashboard.instance)
        app.CurrentPage.instance.refresh();
};

function showNotifications() {
    if (app.CurrentPage && app.CurrentPage.instance == app.Pages.Notifications.instance) {
        if (app.LeftMenu && app.LeftMenu.instance && app.LeftMenu.instance.isVisible())
            app.LeftMenu.instance.hide();
        return;
    }
    if (!app.Pages.Notifications.instance)
        app.Pages.Notifications.instance = new NotificationsPage(app.Pages.Notifications.container);
    app.changePage(app.Pages.Notifications);
    app.Pages.Notifications.instance.refresh();
};

function goBack() {
    if (app)
        app.goBack();
    else history.back();
};

var commandExecuted = function(cmd) {
    Event.sendEvent(null, 'commandExecuted', { Command: cmd });
};

var setDeviceId = function (deviceId) {
    app.User.DeviceId = deviceId;
    app.registerDeviceId();
};

function authenticate(server, username, password) {
    if (!server || !username || !password || !app.Pages.Authentication.instance)
        return;

    username = server + '\\' + username;
    app.Pages.Authentication.instance.authenticate(username, password);
};

var isInDevelopment = $("#InDevelopment").val() == 'true';

var log = function (message, shouldAlert) {
    if (!isInDevelopment)
        return;

    if (shouldAlert)
        alert(message);

    console.log(message);
};